package com.buss.controller;

import com.baomidou.kisso.web.waf.request.WafRequestWrapper;
import com.buss.model.UploadFileLine;
import com.buss.storage.StorageFileNotFoundException;
import com.buss.util.Constant;
import com.buss.util.map.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class FileUploadController extends BaseFileController {

    /**
     * 跳转页面，上传演示页面
     */
    @GetMapping("/demo")
    public String listUploadedFiles(Model model) throws IOException {
        model.addAttribute("repo", repo);
        return "uploadForm";
    }
    /**
     * 跳转页面，文件打包上传，例如app上传
     */
    @GetMapping("/demo2")
    public String listUploadedFilesApp(Model model) throws IOException {
        model.addAttribute("repo", repo);
        return "uploadFormExtend";
    }

    /**
     * 1.1  文件上传，支持多文件，其他文件
     */
    @PostMapping("/")
    @ResponseBody
    public Map<String, Object> upload3 (HttpServletRequest request) {
        Map<String, Object> response = handleFileUploadSimple(request, Constant.COMMON_RETURN_KEY);
        return response;
    }

    /**
     * 1.2  删除 已上传的附件流水
     */
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete (HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String,Object>();
        return response;
    }
    /**
     * 1.3  查询 已上传的附件流水
     */
    @RequestMapping("/line")
    @ResponseBody
    public Map<String, Object> query (HttpServletRequest request) {
        WafRequestWrapper wr = new WafRequestWrapper(request);
        String FJIDBACK = wr.getParameter("FJIDBACK"); //附件ID

        Example example = new Example(UploadFileLine.class);
        // 查询条件拼装
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isBlank(FJIDBACK)){
            criteria.andEqualTo("fjidback", FJIDBACK);
        }
        example.orderBy("crtDatetime").desc(); //排序(按创建时间降序)
        List<UploadFileLine> list = service.selectByExample(example);

        Map<String, Object> response = new HashMap<String,Object>();
        response.put("datalist", list);
        UploadFileLine form = new UploadFileLine();
        if(!StringUtils.isBlank(FJIDBACK)){
            form.setFjidback(FJIDBACK);
        }
        int total = service.selectCount(form);
        response.put("total", total);
        response.put(Constant.COMMON_RETURN_KEY, new Result().success());
        return response;
    }

    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
        return ResponseEntity.notFound().build();
    }

}
